package nc.hw;

import java.util.*;

public class HJ25 {

    private static String output(int[] numsI, int[] numsR) {
        Map<Integer, List<Integer>> outputI = new HashMap<>();
        Arrays.sort(numsR);
        int totalNum = 0;
        List<Integer> outR = new ArrayList<>();
        for (int i : numsR) {
            if (!outputI.containsKey(i)) {
                ArrayList<Integer> value = new ArrayList<>();
                outputI.put(i, value);
                int hasI = 0;
                for (int i1 = 0; i1 < numsI.length; i1++) {
                    if (String.valueOf(numsI[i1]).contains(String.valueOf(i))) {
                        value.add(i1);
                        value.add(numsI[i1]);
                        totalNum += 2;
                        hasI = 2;
                    }
                }
                if (hasI == 2) {
                    outR.add(i);
                }
                totalNum += hasI;
            }
        }
        StringBuilder sb = new StringBuilder();
        sb.append(totalNum);
        for (Integer integer : outR) {
            List<Integer> integers = outputI.get(integer);
            sb.append(" ").append(integer).append(" ").append(integers.size() / 2);
            for (Integer integer1 : integers) {
                sb.append(" ").append(integer1);
            }
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        System.out.println(output(
                new int[]{
                        123,456,786,453,46,7,5,3,665,453456,745,456,786,453,123
                },
                new int[]{
                        6,3,6,3,0
                }
        ));
    }

}
